package com.xsjt.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.xsjt.bean.Role;
import com.xsjt.bean.User;
import com.xsjt.mapper.RoleMapper;
import com.xsjt.mapper.UserMapper;
import com.xsjt.service.UserService;

/**
 * 服务层
 * @author Administrator
 *
 */
@Service
public class UserServiceImpl implements UserService {
	
	// 注入 dao
	@Resource
    private UserMapper userMapper;
	@Resource
	private RoleMapper roleMapper;
	
	
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public boolean addUserAndRole(User user) {
		boolean flag1 = userMapper.addUser(user);
		Role role = new Role();
		role.setRolename(user.getRolename());
		boolean flag2 = roleMapper.addRole(role);
		
		return flag1 && flag2;
	}
	

	@Override
	public List<User> findUsers(Map<String, Object> map) {
		int pageNum = (int) map.get("pageNum");
		int pageSize = (int) map.get("pageSize");
		// 第一个参数是第几页；第二个参数是每页显示条数
		PageHelper.startPage(pageNum, pageSize);
		return userMapper.findUsers(map);
	}

}
